package verify

import (
	"crypto/md5"
	"crypto/rand"
	"encoding/base64"
	"encoding/binary"
	"encoding/hex"
	"time"
)

const (
	ProductSceretKeyRandLength = 32
)

func getRandBytesBase64(len uint32) string {
	randBytes := make([]byte, len)
	if _, err := rand.Read(randBytes); err != nil {
		return ""
	}

	return base64.URLEncoding.EncodeToString(randBytes)
}

func getNowTimeBase64() string {
	timeBytes := make([]byte, binary.MaxVarintLen64)
	binary.BigEndian.PutUint64(timeBytes, uint64(time.Now().Unix()))

	return base64.URLEncoding.EncodeToString(timeBytes)
}

/*
1 随机生成32 byte的randBytes
2 对randBytes做base64编码
*/
func ProductNewSceretKey() string {
	randBase64 := getRandBytesBase64(ProductSceretKeyRandLength)
	timeBase64 := getNowTimeBase64()

	h := md5.New()
	h.Write(([]byte)(randBase64 + timeBase64))
	return hex.EncodeToString(h.Sum(nil))
}
